package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TIconEditConfig;
	import com.NeuglsWorkStudio.Utility.Utility;
	import com.NeuglsWorkStudio.Utility.nConsts;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	public class TIconEdit extends TEdit
	{
		private var _FIcon:DisplayObject;
		private var _FIconContainer:Sprite;
		
		public function TIconEdit(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public function get Icon():DisplayObject
		{
			return _FIcon;
		}

		public function set Icon(value:DisplayObject):void
		{
			if(value==null) return;
			if(_FIcon!=null&&_FIconContainer.contains(_FIcon))
				_FIconContainer.removeChild(_FIcon);
			_FIcon = value;
			_FIconContainer.addChild(_FIcon);
			
			Paint();
		}
		
		private function IconEditConfig():TIconEditConfig{
			return _FConfig as TIconEditConfig;
		}

		override protected function Init():void{
			super.Init();
			Editor.border=false;
			Editor.addEventListener(TextEvent.TEXT_INPUT,function(E:Event):void{
				if(IconEditConfig().OnEditorValueChanged!=null)
					IconEditConfig().OnEditorValueChanged(Editor.text);
			},false,0,true);
			
			Editor.addEventListener(KeyboardEvent.KEY_UP,onKeyUp,false,0,true);
			
			_FIconContainer=new Sprite();
			_FIconContainer.buttonMode=true;
			_FIconContainer.addEventListener(MouseEvent.CLICK,FOnIconClick);
			_FIconContainer.addEventListener(Event.REMOVED,function(E:Event):void{
				this.removeEventListener(MouseEvent.CLICK,FOnIconClick);				
			});
			
			this.addChild(_FIconContainer);
			Icon=IconEditConfig().Icon;
		}
		
		private function onKeyUp(e:KeyboardEvent):void{
			if (e.keyCode == 10||e.keyCode == 13)
			{			
				if(Editor.text=='')
				{
					return;
				}
				
				if(IconEditConfig().OnAcceptUserInput!=null){
					IconEditConfig().OnAcceptUserInput(Editor.text);
				}
			}
		}
		
		private function FOnIconClick(E:MouseEvent):void{
			if(IconEditConfig().OnIconClick!=null)
				IconEditConfig().OnIconClick();
		}
		
		protected function Paint():void{
			var LastX:Number=0;
			var LastY:Number=0;
			var W:Number=IconEditConfig().Width;
			var H:Number=IconEditConfig().Height;
			var Margin:Number=3;
			if(IconEditConfig().DisplayBorder){
				var BorderSize:Number=1;
				with(this.graphics){
					lineStyle(BorderSize,ColorUtility.GetColorFromBaseColor(GetEditConfig().BaseColor));
					drawRoundRect(0,0,W-2*BorderSize,H-BorderSize*2,6,6);
					endFill();
					LastX+=BorderSize;
					LastY+=BorderSize;
					W-=2*BorderSize;
					H-=2*BorderSize;
				}
			}
			Editor.width=W-_FIcon.width-Margin;
			Editor.height=H;
			Editor.x=LastX;
			Editor.y=LastY;
			_FIconContainer.x=W-_FIconContainer.width-3;
			_FIconContainer.y=(H-_FIconContainer.height)/2;
		}
	}
}